﻿package com.wxsoft.business.sys.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.business.sys.dao.ISysRolesMenuDao;
import com.wxsoft.business.sys.pojo.SysRolesMenu;
import com.wxsoft.business.sys.pojo.SysUser;
import com.wxsoft.business.sys.service.ISysRolesMenuService;
import com.wxsoft.util.wx.UUIDUtil;

@Service("sysRolesMenuService")
public class SysRolesMenuServiceImpl implements ISysRolesMenuService {
	@Resource
	private ISysRolesMenuDao dao;

	public long findCount(SysRolesMenu sysRolesMenu) {
		return dao.findCount(sysRolesMenu);
	}

	public List<SysRolesMenu> findAll(PageHelper page, SysRolesMenu sysRolesMenu) {
		List<SysRolesMenu> r = dao.findAll(page, sysRolesMenu);
		return r;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public String add(SysRolesMenu sysRolesMenu) {
		sysRolesMenu.setId(UUIDUtil.getUUID());
		this.dao.insert(sysRolesMenu);
		return "操作成功!";
	}

	public String modify(SysRolesMenu sysRolesMenu) {
		this.dao.modify(sysRolesMenu);
		return "操作成功!";
	}

	public String delete(SysRolesMenu sysRolesMenu) {
		this.dao.delete(sysRolesMenu);
		return "操作成功!";
	}

	/**
	 * 给角色 添加功能 
	 */
	@Transactional
	public String addForRole(SysRolesMenu sysRolesMenu, SysUser user, String menuIds) {

		sysRolesMenu.setUnit_id(user.getUnit_id());
		sysRolesMenu.setHandle(user.getUser_id());
		sysRolesMenu.setHandle_date(new Date());
		//删除这个单位 角色下的所有权限
		SysRolesMenu srmForD = new SysRolesMenu();
		srmForD.setUnit_id(sysRolesMenu.getUnit_id());
		srmForD.setRole_code(sysRolesMenu.getRole_code());
//		List<SysRolesMenu> oldsrmL = dao.getBy(srmForQ);
		//删除所有旧权限
		int rDel = dao.delete(srmForD);
		
		String[] menuIdArr = menuIds.split(",");
		for (String menuId : menuIdArr) {
			sysRolesMenu.setId(UUIDUtil.getUUID());
			sysRolesMenu.setCode(menuId);
			
			this.dao.insert(sysRolesMenu);
		}
		// TODO Auto-generated method stub
		return "操作成功!";
	}

}
